<template>
  <div class="mod-config">
      <el-form inline  v-show="searchShow" ref="searchForm"  :model="dataForm" @keyup.enter.native="searchChange()">
        <el-form-item label="付款单号" prop="code">
          <el-input v-model="dataForm.billId" placeholder="付款单号"></el-input>
        </el-form-item>
        <el-form-item label="制造商">
          <el-input v-model="dataForm.supplyName" readonly>
            <el-button slot="append" @click="checkSupplyList">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="制单人" prop="createUserName">
          <el-input v-model="dataForm.createUserName" placeholder="制单人"></el-input>
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select v-model="dataForm.status" clearable>
            <el-option label="未审核" value="0"></el-option>
            <el-option label="已审核" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="付款单日期">
          <el-row>
            <el-col :span="11">
              <el-date-picker v-model="dataForm.createTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-col>
            <el-col class="line" :span="2" style="text-align: center;">-</el-col>
            <el-col :span="11">
              <el-date-picker v-model="dataForm.createTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="审核日期">
          <el-row>
            <el-col :span="11">
              <el-date-picker v-model="dataForm.auditTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-col>
            <el-col class="line" :span="2" style="text-align: center;">-</el-col>
            <el-col :span="11">
              <el-date-picker v-model="dataForm.auditTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-col>
          </el-row>
        </el-form-item>
        
        <el-form-item label="部门">
          <el-input v-model="dataForm.deptName" readonly>
            <el-button slot="append" @click="openDeptDialog">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="单据类型">
          <el-select v-model="dataForm.type" clearable>
            <el-option label="采购付款" value="1"></el-option>
            <el-option label="预付款" value="2"></el-option>
            <el-option label="厂商减账" value="3"></el-option>
            <el-option label="调账" value="4"></el-option>
            <el-option label="其他付款" value="5"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="searchChange">查询</el-button>
          <el-button type="default" icon="el-icon-delete" @click="searchReset">清空</el-button>
        </el-form-item>

      </el-form>
      <el-row>
      <el-col :span="24">
        <el-button v-if="permissions.eims_payslip_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button icon="el-icon-search" circle @click="handleSearchShow" style="float:right;"></el-button>
        <el-button icon="el-icon-refresh" circle @click="refreshChange" style="float:right;"></el-button>
      </el-col>
      </el-row>
      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading">
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-edit" @click="getDetail(scope.row.id)">详情</el-button>
              <el-button v-if="permissions.eims_payslip_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)" :disabled="scope.row.status == 1">修改</el-button>
              <el-button v-if="permissions.eims_payslip_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)" :disabled="scope.row.status == 1">删除</el-button>
              <el-button v-if="permissions.eims_payslip_edit" type="text" icon="el-icon-edit" @click="cancelAudit(scope.row.id)" :disabled="scope.row.status != 1">反审核</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="billId" label="付款单号"></el-table-column>
          <el-table-column label="制造商/部门名称">
            <template slot-scope="scope">
              <span v-if="scope.row.payType == 1">{{scope.row.supplyName}}</span>
              <span v-if="scope.row.payType == 2">{{scope.row.deptName}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间"></el-table-column>    
          <el-table-column prop="type" label="单据类型" :formatter="typeFormatter"></el-table-column>
          <el-table-column prop="auditTime" label="审核日期"></el-table-column>
          <el-table-column prop="auditUserName" label="审核人"></el-table-column>
          <el-table-column prop="writeOffAmount" label="本次核销金额"></el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next">
        </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
      <!-- 弹窗, 详情 -->
      <detail-form v-if="detailVisible" ref="detail" @refreshDataList="getDataList"></detail-form>
      <!-- 弹窗, 制造商查询 -->
    <supply-search v-if="supplySearchVisible" ref="supplySearch" @refreshData="renewFormData"></supply-search>
    <!-- 弹窗, 部门查询 -->
    <search-dept v-if="deptVisible" ref="deptDialog" @refreshData="getDeptData"></search-dept>
  </div>
</template>

<script>
  import {fetchList, delObj,cancelAuditById} from '@/api/eims/payslip'
  import TableForm from './payslip-form'
  import DetailForm from './detail-form'
  import SupplySearch from "@/components/business/SearchSupply"
  import SearchDept from './SearchDept'
  import { filterParams } from '@/util/tools'
  import {mapGetters} from 'vuex'
  export default {
    data () {
      return {
        dataForm: {
          supplyName: '',
          deptName: ''
        },
        dataList: [],
        searchShow:true,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        detailVisible: false,
        supplySearchVisible: false,
        deptVisible: false
      }
    },
    components: {
      TableForm,
      DetailForm,
      SupplySearch,
      SearchDept
    },
    created () {
      this.getDataList()
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        let params1 = {
          current: this.pageIndex,
          size: this.pageSize
        }
        let params2 = filterParams(this.dataForm)
        fetchList(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      //详情
      getDetail(id){
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.detail.init(id)
        })
      },
      //反审核
      cancelAudit(id) {
        this.$confirm('是否确认反审核ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return cancelAuditById(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 删除
      deleteHandle (id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delObj(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      searchChange() {
        this.pageIndex=1;
        this.getDataList()
      },
      // 搜索清空
      searchReset () {
        for (let key in this.dataForm) {
          this.dataForm[key] = ''
        }
      },
      handleSearchShow () {
        this.searchShow = !this.searchShow;
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getDataList(this.page)
      },
      typeFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case "1":
            return '采购付款'
            break
          case "2":
            return '预付款'
            break
          case "3":
            return '厂商减账'
            break
          case "4":
            return '调账'
            break
          case "5":
            return '其他付款'
            break
        }
      },
      payTypeFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case "1":
            return '按制造商'
            break
          case "2":
            return '按部门'
            break
        }
      },
      //选择制造商
      checkSupplyList() {
        let data = {};
        this.supplySearchVisible = true;
        this.$nextTick(() => {
          this.$refs.supplySearch.init(data);
        });
      },
      // 选择制造商
      renewFormData(data) {
        this.dataForm.supplyId = data.id;
        this.dataForm.supplyName = data.name;
      },
      //  打开查询部门弹窗
      openDeptDialog() {
        this.deptVisible = true
        this.$nextTick(() => {
          this.$refs.deptDialog.init()
        })
      },
      //  获取选择部门数据
      getDeptData(data) {
        this.dataForm.deptName = data[0].name
        this.dataForm.deptId = data[0].id
      },
    }
  }
</script>
